﻿using System;
using CobraCms.Core.Content;
using CobraCms.Tests.ContentDefinitions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace CobraCms.Tests
{
    [TestClass]
    public class ReflectionTests
    {
        [TestMethod]
        public void DefinitionIndexBuilderTest()
        {
            var index = new ReflectedDefinitionIndex();
            index.BuildIndex();

            var rootNodes = index.ListRootContentNodeDefinitions();
            Assert.AreEqual(1, rootNodes.Count, "1 root node not returned");

            var topLevelNodes = index.ListAllowedChildContentNodeDefinitions(rootNodes[0].DefinitionSettings.DefinitionId);
            Assert.IsTrue(topLevelNodes.Count > 1, "Should have multiple top-level nodes");
        }

        [TestMethod]
        public void TypedContentMapperTest()
        {
            var populator = new TypedContentMapper<ExhaustiveDefinition>();

            var testString = "TestString";
            var testInt32 = 32;
            var testInt64 = 64L;
            var testDecimal = 3.14159m;
            var testDateTime = new DateTime(1981, 5, 31, 11, 15, 20);
            var data = new ContentNode();
            data.PropertyData.Add("StringProperty", new ContentNodeProperty { PropertyName = "StringProperty", ValueAsString = testString });
            data.PropertyData.Add("Int32Property", new ContentNodeProperty { PropertyName = "Int32Property", ValueAsInt32 = testInt32 });
            data.PropertyData.Add("Int64Property", new ContentNodeProperty { PropertyName = "Int64Property", ValueAsInt64 = testInt64 });
            data.PropertyData.Add("DecimalProperty", new ContentNodeProperty { PropertyName = "DecimalProperty", ValueAsDecimal = testDecimal });
            data.PropertyData.Add("DateTimeProperty", new ContentNodeProperty { PropertyName = "DateTimeProperty", ValueAsDateTime = testDateTime });

            var testObject = new ExhaustiveDefinition();            
            populator.ToType(data, testObject);

            Assert.AreEqual(testString, testObject.StringProperty);
            Assert.AreEqual(testInt32, testObject.Int32Property);
            Assert.AreEqual(testInt64, testObject.Int64Property);
            Assert.AreEqual(testDecimal, testObject.DecimalProperty);
            Assert.AreEqual(testDateTime, testObject.DateTimeProperty);


            var targetData = new ContentNode();
            populator.FromType(testObject, targetData);
            Assert.IsNotNull(targetData.PropertyData["StringProperty"]);
            Assert.AreEqual(testString, targetData.PropertyData["StringProperty"].ValueAsString);

            Assert.IsNotNull(targetData.PropertyData["Int32Property"]);
            Assert.AreEqual(testInt32, targetData.PropertyData["Int32Property"].ValueAsInt32);

            Assert.IsNotNull(targetData.PropertyData["Int64Property"]);
            Assert.AreEqual(testInt64, targetData.PropertyData["Int64Property"].ValueAsInt64);

            Assert.IsNotNull(targetData.PropertyData["DecimalProperty"]);
            Assert.AreEqual(testDecimal, targetData.PropertyData["DecimalProperty"].ValueAsDecimal);

            Assert.IsNotNull(targetData.PropertyData["DateTimeProperty"]);
            Assert.AreEqual(testDateTime, targetData.PropertyData["DateTimeProperty"].ValueAsDateTime);


        }
    }
}
